// Copyright 2019 Proyectos y Sistemas de Mantenimiento SL (eProsima).
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ------------------------------------------------------------------
// Modification history:
// feature: support config log level by cmd tool
// feature: add statictic lookup
// ------------------------------------------------------------------

#ifndef INCLUDE_EDDS_DDS_BUILTIN_LOOKUP_COMMON_LOOKUPTYPES_HPP_
#define INCLUDE_EDDS_DDS_BUILTIN_LOOKUP_COMMON_LOOKUPTYPES_HPP_

#include <ertps/types/TypeObject.h>

#include <cstdint>
#include <vector>
#include <edds/rtps/common/SerializedPayload.h>
#include <ecdr/Cdr.h>
#include <ecdr/exceptions/BadParamException.h>
#include <edds/dds/builtin/common/RequestHeader.hpp>
#include <edds/dds/core/policy/QosPolicies.hpp>
#include <edds/rtps/common/LocatorSelectorEntry.hpp>
#include <elog/statistics/Statistics.hpp>
#include <typeinfo>
#include <discovery/database/DiscoveryParticipantInfo.hpp>
#include <discovery/database/DiscoveryEndpointInfo.hpp>

namespace evbs {
namespace edds {
namespace dds {
namespace builtin {

using namespace vbsutil;
using namespace vbsutil::xmlparser;
using namespace vbsutil::elog;

const int32_t Lookup_getTypeInfo_Hash = static_cast<int32_t>(0x15a2d238);
const int32_t Lookup_getTypes_Hash = static_cast<int32_t>(0xd35282d1);
const int32_t Lookup_getDependencies_Hash = static_cast<int32_t>(0x31fbaa35);
const int32_t Statistic_getWRInfo_Hash = static_cast<int32_t>(0x40001001);
const int32_t Statistic_getMatchGuids_Hash = static_cast<int32_t>(0x40001002);
const int32_t Statistic_getSendInfo_Hash = static_cast<int32_t>(0x40001003);
const int32_t Statistic_getRecvInfo_Hash = static_cast<int32_t>(0x40001004);
const int32_t Statistic_getWriterReaderQos_Hash = static_cast<int32_t>(0x40001005);
const int32_t Statistic_getMsgBrief_Hash = static_cast<int32_t>(0x40001006);
const int32_t Statistic_getProxyInfo_Hash = static_cast<int32_t>(0x40001007);
const int32_t Statistic_remoteConfig_Hash = static_cast<int32_t>(0x40001008);
const int32_t Statistic_getDServerInfo_Hash = static_cast<int32_t>(0x40001009);

enum StatisticEntityType : uint8_t { STATICTIC_ENTITY_WRITER, STATICTIC_ENTITY_READER };

struct WriterReaderInfo {
    StatisticEntityType type;
    int32_t matched;  // 匹配的对端个数
    ertps::rtps::GUID_t guid;
    std::string topic_name;
    std::string type_name;

    RTPS_DllAPI static size_t getCdrSerializedSize(const WriterReaderInfo& data, size_t current_alignment = 0);

    RTPS_DllAPI void serialize(vbsutil::ecdr::Cdr& cdr) const;

    RTPS_DllAPI void deserialize(vbsutil::ecdr::Cdr& cdr);
};

struct ProxyInfo {
    StatisticEntityType type;

    // common proxy info
    bool is_alive;
    int64_t start_time;

    // writer proxy
    uint32_t last_heartbeat_count;
    uint64_t last_notified;
    uint64_t available_changes_max;  // 收到消息的最大可用sequence
    uint64_t max_sequence_number;    // writer通知的消息的最大sequence
    ertps::rtps::LocatorSelectorEntry writer_proxy_locators_entry {4, 1};

    // reader proxy
    uint32_t last_acknack_count;
    uint32_t last_nackfrag_count;
    uint64_t changes_low_mark;  // the highest fully acknowledged sequence number.
    ertps::rtps::LocatorSelectorEntry reader_proxy_general_locator_entry {4, 1};
    ertps::rtps::LocatorSelectorEntry reader_proxy_async_locator_entry {4, 1};

    RTPS_DllAPI static size_t getCdrSerializedSize(const ProxyInfo& data, size_t current_alignment = 0);

    RTPS_DllAPI void serialize(vbsutil::ecdr::Cdr& cdr) const;

    RTPS_DllAPI void deserialize(vbsutil::ecdr::Cdr& cdr);
};

using StatisticWrtierReaderInfos = std::vector<WriterReaderInfo>;
using StatisticMatchGuids = std::vector<evbs::ertps::rtps::GUID_t>;
using StatisticProxyInfos = std::vector<ProxyInfo>;

struct StatisticSendInfo {
    uint32_t send_count = 0u;               // 发送总个数
    uint32_t send_throughput = 0u;          // 发送吞吐量
    uint32_t send_payload_length_avg = 0u;  // 自上次查询发送失败的个数，每次查询时重置为0
    int64_t last_pkt_timestamp;
    int64_t first_pkt_timestamp;

    RTPS_DllAPI static size_t getCdrSerializedSize(const StatisticSendInfo& data, size_t current_alignment = 0);

    RTPS_DllAPI void serialize(vbsutil::ecdr::Cdr& cdr) const;

    RTPS_DllAPI void deserialize(vbsutil::ecdr::Cdr& cdr);
};

struct StatisticRecvInfo {
    vbsutil::statistics::u64ByGuid recv_count;
    vbsutil::statistics::u64ByGuid lost_count;
    vbsutil::statistics::u64ByGuid avg_latancy;
    vbsutil::statistics::u64ByGuid avg_throughput;
    vbsutil::statistics::i64ByGuid last_pkt_timestamp;
    vbsutil::statistics::i64ByGuid first_pkt_timestamp;
    uint32_t take_count_total = 0u;  // 用户take的总个数
    uint32_t untake_count = 0u;      // 队列中未take的个数
    uint32_t rejected_count = 0u;    // 适配MVBS

    RTPS_DllAPI static size_t getCdrSerializedSize(const StatisticRecvInfo& data, size_t current_alignment = 0);

    RTPS_DllAPI void serialize(vbsutil::ecdr::Cdr& cdr) const;

    RTPS_DllAPI void deserialize(vbsutil::ecdr::Cdr& cdr);
};

struct StatisticWriterReaderQos {
    DurabilityQosPolicyKind durability;
    Duration_t deadline;
    LivelinessQosPolicyKind liveliness_kind;
    Duration_t liveliness_lease_duration;
    Duration_t liveliness_announcement_period;
    ReliabilityQosPolicyKind reliability_kind;
    Duration_t reliability_max_blocking_time;
    HistoryQosPolicyKind history_kind;
    int32_t history_depth;
    int32_t max_samples;
    int32_t max_instances;
    int32_t max_samples_per_instance;
    int32_t allocated_samples;
    int32_t extra_samples;
    Duration_t lifespan;
    OwnershipQosPolicyKind ownership;
    PublishModeQosPolicyKind pub_mode;
    std::string flowctlname;
    bool e2e_enable;
    bool send_multi_enable;

    RTPS_DllAPI static size_t getCdrSerializedSize(const StatisticWriterReaderQos& data, size_t current_alignment = 0);

    RTPS_DllAPI void serialize(vbsutil::ecdr::Cdr& cdr) const;

    RTPS_DllAPI void deserialize(vbsutil::ecdr::Cdr& cdr);
};

struct Statistic_getInfoByGuid_Req {
    ertps::rtps::GUID_t guid_;

    RTPS_DllAPI static size_t getCdrSerializedSize(const Statistic_getInfoByGuid_Req& data,
                                                   size_t current_alignment = 0);

    RTPS_DllAPI void serialize(vbsutil::ecdr::Cdr& cdr) const;

    RTPS_DllAPI void deserialize(vbsutil::ecdr::Cdr& cdr);

    RTPS_DllAPI static bool isKeyDefined() { return false; }
};

struct Lookup_getTypeInfo_Req {
    std::string type_name;

    RTPS_DllAPI static size_t getCdrSerializedSize(const Lookup_getTypeInfo_Req& data, size_t current_alignment = 0);

    RTPS_DllAPI void serialize(vbsutil::ecdr::Cdr& cdr) const;

    RTPS_DllAPI void deserialize(vbsutil::ecdr::Cdr& cdr);

    RTPS_DllAPI static bool isKeyDefined() { return false; }
};

struct Lookup_getTypes_Req {
    std::vector<ertps::types::TypeIdentifier> type_ids;

    RTPS_DllAPI static size_t getCdrSerializedSize(const Lookup_getTypes_Req& data, size_t current_alignment = 0);

    RTPS_DllAPI void serialize(vbsutil::ecdr::Cdr& cdr) const;

    RTPS_DllAPI void deserialize(vbsutil::ecdr::Cdr& cdr);

    RTPS_DllAPI static bool isKeyDefined() { return false; }
};

class Lookup_getTypeDependencies_Req {
 public:
    std::vector<ertps::types::TypeIdentifier> type_ids;

    std::vector<uint8_t> continuation_point;

    RTPS_DllAPI static size_t getCdrSerializedSize(const Lookup_getTypeDependencies_Req& data,
                                                   size_t current_alignment = 0);

    RTPS_DllAPI void serialize(vbsutil::ecdr::Cdr& cdr) const;

    RTPS_DllAPI void deserialize(vbsutil::ecdr::Cdr& cdr);

    RTPS_DllAPI static bool isKeyDefined() { return false; }
};

struct Lookup_remoteConfig_Req {
    std::string param_name;
    std::string host_name;
    uint32_t pid;

    RTPS_DllAPI static size_t getCdrSerializedSize(const Lookup_remoteConfig_Req& data, size_t current_alignment = 0);

    RTPS_DllAPI void serialize(vbsutil::ecdr::Cdr& cdr) const;

    RTPS_DllAPI void deserialize(vbsutil::ecdr::Cdr& cdr);
};

struct Lookup_getWriterReaderInfo_Result {
    StatisticWrtierReaderInfos infos_;

    RTPS_DllAPI static size_t getCdrSerializedSize(const Lookup_getWriterReaderInfo_Result& data,
                                                   size_t current_alignment = 0);

    RTPS_DllAPI void serialize(vbsutil::ecdr::Cdr& cdr) const;

    RTPS_DllAPI void deserialize(vbsutil::ecdr::Cdr& cdr);

    RTPS_DllAPI static bool isKeyDefined() { return false; }
};

struct Lookup_getMatchGuids_Result {
    StatisticMatchGuids guids_;

    RTPS_DllAPI static size_t getCdrSerializedSize(const Lookup_getMatchGuids_Result& data,
                                                   size_t current_alignment = 0);

    RTPS_DllAPI void serialize(vbsutil::ecdr::Cdr& cdr) const;

    RTPS_DllAPI void deserialize(vbsutil::ecdr::Cdr& cdr);

    RTPS_DllAPI static bool isKeyDefined() { return false; }
};

struct Lookup_getSendInfo_Result {
    StatisticSendInfo info_;

    RTPS_DllAPI static size_t getCdrSerializedSize(const Lookup_getSendInfo_Result& data, size_t current_alignment = 0);

    RTPS_DllAPI void serialize(vbsutil::ecdr::Cdr& cdr) const;

    RTPS_DllAPI void deserialize(vbsutil::ecdr::Cdr& cdr);

    RTPS_DllAPI static bool isKeyDefined() { return false; }
};

struct Lookup_getRecvInfo_Result {
    StatisticRecvInfo info_;

    RTPS_DllAPI static size_t getCdrSerializedSize(const Lookup_getRecvInfo_Result& data, size_t current_alignment = 0);

    RTPS_DllAPI void serialize(vbsutil::ecdr::Cdr& cdr) const;

    RTPS_DllAPI void deserialize(vbsutil::ecdr::Cdr& cdr);

    RTPS_DllAPI static bool isKeyDefined() { return false; }
};

struct Lookup_getWriterQos_Result {
    StatisticWriterReaderQos qos_;

    RTPS_DllAPI static size_t getCdrSerializedSize(const Lookup_getWriterQos_Result& data,
                                                   size_t current_alignment = 0);

    RTPS_DllAPI void serialize(vbsutil::ecdr::Cdr& cdr) const;

    RTPS_DllAPI void deserialize(vbsutil::ecdr::Cdr& cdr);

    RTPS_DllAPI static bool isKeyDefined() { return false; }
};

struct Lookup_getTypeInfo_Result {
    std::string type_name;
    ertps::types::TypeInformation information;

    RTPS_DllAPI static size_t getCdrSerializedSize(const Lookup_getTypeInfo_Result& data, size_t current_alignment = 0);

    RTPS_DllAPI void serialize(vbsutil::ecdr::Cdr& cdr) const;

    RTPS_DllAPI void deserialize(vbsutil::ecdr::Cdr& cdr);

    RTPS_DllAPI static bool isKeyDefined() { return false; }
};

struct Lookup_getTypes_Result {
    std::vector<ertps::types::TypeIdentifierTypeObjectPair> types;

    std::vector<ertps::types::TypeIdentifierPair> complete_to_minimal;

    RTPS_DllAPI static size_t getCdrSerializedSize(const Lookup_getTypes_Result& data, size_t current_alignment = 0);

    RTPS_DllAPI void serialize(vbsutil::ecdr::Cdr& cdr) const;

    RTPS_DllAPI void deserialize(vbsutil::ecdr::Cdr& cdr);

    RTPS_DllAPI static bool isKeyDefined() { return false; }
};

class Lookup_getTypeDependencies_Result {
 public:
    std::vector<ertps::types::TypeIdentifierWithSize> dependent_typeids;

    std::vector<uint8_t> continuation_point;

    RTPS_DllAPI static size_t getCdrSerializedSize(const Lookup_getTypeDependencies_Result& data,
                                                   size_t current_alignment = 0);

    RTPS_DllAPI void serialize(vbsutil::ecdr::Cdr& cdr) const;

    RTPS_DllAPI void deserialize(vbsutil::ecdr::Cdr& cdr);

    RTPS_DllAPI static bool isKeyDefined() { return false; }
};

struct Lookup_getMsgBrief_Result {
    Log::GuidMessageBriefsMap guid_msg_briefs_;

    RTPS_DllAPI static size_t getCdrSerializedSize(const Lookup_getMsgBrief_Result& data, size_t current_alignment = 0);

    RTPS_DllAPI void serialize(vbsutil::ecdr::Cdr& cdr) const;

    RTPS_DllAPI void deserialize(vbsutil::ecdr::Cdr& cdr);
};

struct Lookup_getProxyInfos_Result {
    StatisticProxyInfos proxy_infos_;

    RTPS_DllAPI static size_t getCdrSerializedSize(const Lookup_getProxyInfos_Result& data,
                                                   size_t current_alignment = 0);

    RTPS_DllAPI void serialize(vbsutil::ecdr::Cdr& cdr) const;

    RTPS_DllAPI void deserialize(vbsutil::ecdr::Cdr& cdr);
};

struct Lookup_remoteConfig_Result {
    uint32_t result_ = 0;
};

struct Lookup_getDServerInfo_Result {
    std::string infos_;

    RTPS_DllAPI static size_t getCdrSerializedSize(const Lookup_getDServerInfo_Result& data,
                                                   size_t current_alignment = 0) {
        return 4 + data.infos_.size() + vbsutil::ecdr::Cdr::alignment(current_alignment, 4U);
    }

    RTPS_DllAPI void serialize(vbsutil::ecdr::Cdr& cdr) const { cdr << infos_; }

    RTPS_DllAPI void deserialize(vbsutil::ecdr::Cdr& cdr) { cdr >> infos_; }
};

class Lookup_Call {
 public:
    RTPS_DllAPI Lookup_Call();

    RTPS_DllAPI ~Lookup_Call();

    RTPS_DllAPI Lookup_Call(const Lookup_Call& x);

    RTPS_DllAPI Lookup_Call& operator=(const Lookup_Call& x);

    RTPS_DllAPI int32_t& _d();

    RTPS_DllAPI void getGuidInfo(const Statistic_getInfoByGuid_Req& _getGuidInfo, int32_t hash_id);

    RTPS_DllAPI const Statistic_getInfoByGuid_Req& getGuidInfo() const;

    RTPS_DllAPI void getTypeInfo(const Lookup_getTypeInfo_Req& _getTypeInfo);

    RTPS_DllAPI const Lookup_getTypeInfo_Req& getTypeInfo() const;

    RTPS_DllAPI void remoteConfig(const Lookup_remoteConfig_Req& _remoteConfigReq, int32_t kind);

    RTPS_DllAPI const Lookup_remoteConfig_Req& remoteConfig() const;

    RTPS_DllAPI void getTypes(const Lookup_getTypes_Req& _getTypes);

    RTPS_DllAPI const Lookup_getTypes_Req& getTypes() const;

    RTPS_DllAPI void getTypeDependencies(const Lookup_getTypeDependencies_Req& _getTypeDependencies);

    RTPS_DllAPI const Lookup_getTypeDependencies_Req& getTypeDependencies() const;

    RTPS_DllAPI static size_t getCdrSerializedSize(const Lookup_Call& data, size_t current_alignment = 0);

    RTPS_DllAPI void serialize(vbsutil::ecdr::Cdr& cdr) const;

    RTPS_DllAPI void deserialize(vbsutil::ecdr::Cdr& cdr);

    RTPS_DllAPI static bool isKeyDefined() { return false; }

 private:
    int32_t m__d;

    Lookup_remoteConfig_Req m_remoteConfig;
    Statistic_getInfoByGuid_Req m_getGuidInfo;
    Lookup_getTypeInfo_Req m_getTypeName;
    Lookup_getTypes_Req m_getTypes;
    Lookup_getTypeDependencies_Req m_getTypeDependencies;
};

class Lookup_Return {
 public:
    RTPS_DllAPI Lookup_Return();

    RTPS_DllAPI ~Lookup_Return();

    RTPS_DllAPI Lookup_Return(const Lookup_Return& x);

    RTPS_DllAPI Lookup_Return& operator=(const Lookup_Return& x);

    RTPS_DllAPI int32_t& _d();

    RTPS_DllAPI void getMsgBreifInfo(const Lookup_getMsgBrief_Result& _getMsgBriefnfo);

    RTPS_DllAPI const Lookup_getMsgBrief_Result& getMsgBreifInfo() const;

    RTPS_DllAPI void getWriterReaderInfo(const Lookup_getWriterReaderInfo_Result& _getWRnfo);

    RTPS_DllAPI const Lookup_getWriterReaderInfo_Result& getWriterReaderInfo() const;

    RTPS_DllAPI void getMatchGuids(const Lookup_getMatchGuids_Result& _getMatchGuids);

    RTPS_DllAPI const Lookup_getMatchGuids_Result& getMatchGuids() const;

    RTPS_DllAPI void getProxyInfos(const Lookup_getProxyInfos_Result& _getMatchGuids);

    RTPS_DllAPI const Lookup_getProxyInfos_Result& getProxyInfos() const;

    RTPS_DllAPI void getSendInfo(const Lookup_getSendInfo_Result& _getMatchGuids);

    RTPS_DllAPI const Lookup_getSendInfo_Result& getSendInfo() const;

    RTPS_DllAPI void getRecvInfo(const Lookup_getRecvInfo_Result& _getMatchGuids);

    RTPS_DllAPI const Lookup_getRecvInfo_Result& getRecvInfo() const;

    RTPS_DllAPI void getTypeInfo(const Lookup_getTypeInfo_Result& _getTypeInfo);

    RTPS_DllAPI const Lookup_getTypeInfo_Result& getTypeInfo() const;

    RTPS_DllAPI void getWriterQos(const Lookup_getWriterQos_Result& _getWriterQos);

    RTPS_DllAPI const Lookup_getWriterQos_Result& getWriterQos() const;

    RTPS_DllAPI void getType(const Lookup_getTypes_Result& _getType);

    RTPS_DllAPI const Lookup_getTypes_Result& getType() const;

    RTPS_DllAPI void getTypeDependencies(const Lookup_getTypeDependencies_Result& _getTypeDependencies);

    RTPS_DllAPI const Lookup_getTypeDependencies_Result& getTypeDependencies() const;

    RTPS_DllAPI void remoteConfig(const Lookup_remoteConfig_Result& _remoteConfig);

    RTPS_DllAPI const Lookup_remoteConfig_Result& remoteConfig() const;

    RTPS_DllAPI void dserverInfo(const Lookup_getDServerInfo_Result& _dserverInfo);

    RTPS_DllAPI const Lookup_getDServerInfo_Result& dserverInfo() const;

    RTPS_DllAPI static size_t getCdrSerializedSize(const Lookup_Return& data, size_t current_alignment = 0);

    RTPS_DllAPI void serialize(vbsutil::ecdr::Cdr& cdr) const;

    RTPS_DllAPI void deserialize(vbsutil::ecdr::Cdr& cdr);

    RTPS_DllAPI static bool isKeyDefined() { return false; }

 private:
    int32_t m__d;

    Lookup_getWriterReaderInfo_Result m_getWRInfo;
    Lookup_getMatchGuids_Result m_getMatchGuids;
    Lookup_getSendInfo_Result m_getSendInfo;
    Lookup_getRecvInfo_Result m_getRecvInfo;
    Lookup_getTypeInfo_Result m_getTypeInfo;
    Lookup_getWriterQos_Result m_getWriterQos;
    Lookup_getTypes_Result m_getType;
    Lookup_getTypeDependencies_Result m_getTypeDependencies;
    Lookup_getMsgBrief_Result m_getMsgBriefInfo;
    Lookup_getProxyInfos_Result m_getProxyInfos;
    Lookup_remoteConfig_Result m_remoteConfig;
    Lookup_getDServerInfo_Result m_dserverInfo;
};

template <typename MsgType>
class Lookup_Rpc {
 public:
    rpc::RequestHeader header;

    MsgType data;

    RTPS_DllAPI static size_t getCdrSerializedSize(const Lookup_Rpc& data, size_t current_alignment = 0) {
        size_t initial_alignment = current_alignment;

        current_alignment += rpc::RequestHeader::getCdrSerializedSize(data.header, current_alignment);
        current_alignment += MsgType::getCdrSerializedSize(data.data, current_alignment);

        return current_alignment - initial_alignment;
    }

    RTPS_DllAPI void serialize(vbsutil::ecdr::Cdr& cdr) const {
        cdr << header;
        cdr << data;
    }

    RTPS_DllAPI void deserialize(vbsutil::ecdr::Cdr& cdr) {
        cdr >> header;
        cdr >> data;
    }

    RTPS_DllAPI static bool isKeyDefined() { return false; }
};

typedef Lookup_Rpc<Lookup_Return> Lookup_Reply;
typedef Lookup_Rpc<Lookup_Call> Lookup_Request;

template <typename Type>
class Lookup_TypeSupport /*: public TypeSupport*/ {
 public:
    RTPS_DllAPI bool serialize(void* data, ertps::rtps::SerializedPayload_t* payload) /*override*/ {
        Type* type = static_cast<Type*>(data);
        vbsutil::ecdr::FastBuffer fastbuffer(reinterpret_cast<char*>(payload->data), payload->max_size);
        vbsutil::ecdr::Cdr ser(fastbuffer, vbsutil::ecdr::Cdr::DEFAULT_ENDIAN, vbsutil::ecdr::Cdr::DDS_CDR);
        payload->encapsulation = ser.endianness() == vbsutil::ecdr::Cdr::BIG_ENDIANNESS ? CDR_BE : CDR_LE;

        try {
            // Serialize encapsulation
            (void)ser.serialize_encapsulation();
            type->serialize(ser);  // Serialize the object:
        } catch (vbsutil::ecdr::exception::Exception& e) {
            elogWarning(STATISTIC_LOOKUP, typeid(Type).name()
                                              << " serialize fail:" << e.what() << " length " << payload->max_size);
            return false;
        }
        payload->length = static_cast<uint32_t>(ser.getSerializedDataLength());  // Get the serialized length
        return true;
    }

    RTPS_DllAPI bool deserialize(ertps::rtps::SerializedPayload_t* payload, void* data) /*override*/ {
        Type* p_type = static_cast<Type*>(data);  // Convert DATA to pointer of your type
        vbsutil::ecdr::FastBuffer fastbuffer(reinterpret_cast<char*>(payload->data),
                                             payload->length);  // Object that manages the raw buffer.
        vbsutil::ecdr::Cdr deser(fastbuffer, vbsutil::ecdr::Cdr::DEFAULT_ENDIAN,
                                 vbsutil::ecdr::Cdr::DDS_CDR);  // Object that deserializes the data.

        try {
            // Deserialize encapsulation.
            (void)deser.read_encapsulation();
            payload->encapsulation = deser.endianness() == vbsutil::ecdr::Cdr::BIG_ENDIANNESS ? CDR_BE : CDR_LE;
            p_type->deserialize(deser);  // Deserialize the object:
        } catch (vbsutil::ecdr::exception::Exception& e) {
            elogWarning(STATISTIC_LOOKUP, typeid(Type).name()
                                              << " deserialize fail:" << e.what() << " length " << payload->length);
            return true;
        }

        return true;
    }

    static size_t getCdrSerializedSize(const Type& data, size_t current_alignment = 0) {
        return Type::getCdrSerializedSize(data, current_alignment);
    }

    RTPS_DllAPI void* create_data() /*override*/ { return new Type(); }

    RTPS_DllAPI void delete_data(void* data) /*override*/ { delete static_cast<Type*>(data); }
};

}  // namespace builtin
}  // namespace dds
}  // namespace edds
}  // namespace evbs

#endif  // INCLUDE_EDDS_DDS_BUILTIN_LOOKUP_COMMON_LOOKUPTYPES_HPP_
